package com.jifeng.sterilizer.controller;

import java.util.Arrays;
import java.util.Map;

import com.jifeng.sterilizer.usercount.MyHttpSessionListener;
import com.jifeng.sterilizer.utils.PageUtils;
import com.jifeng.sterilizer.utils.R;
import com.jifeng.sterilizer.valid.AddGroupValid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jifeng.sterilizer.entity.UserEntity;
import com.jifeng.sterilizer.service.UserService;


/**
 * 用户表
 *
 * @author jifeng
 * @email 1593140625@qq.com
 * @date 2020-07-26 16:33:58
 */
@RestController
@RequestMapping("sterilizer/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 修改当前用户密码 PC
     */
    @PostMapping("/pc/updatePassword")
    public R updatePassword(@RequestBody UserEntity userEntity) {
        R rs=userService.updatePassword(userEntity);
        return rs;
    }

    /**
     * 删除PC管理员 PC
     */
    @PostMapping("/pc/delpcuser")
    public R delPcUser(@RequestBody Map<String, Object> params) {
        R rs=userService.delPcUser(params);
        return rs;
    }

//    /**
//     * 修改PC管理员账号密码 PC
//     */
    @PostMapping("/pc/resetpcuser")
    public R resetPcUser(@RequestBody UserEntity userEntity) {
        R rs=userService.resetPcUser(userEntity);
        return rs;
    }

    /**
     * 添加PC管理员账号列表 PC
     */
    @PostMapping("/pc/addpcuser")
    public R addPcUser(@RequestBody UserEntity userEntity) {
        R rs=userService.addPcUser(userEntity);
        return rs;
    }

    /**
     * PC管理员账号列表 PC
     */
    @GetMapping("/pc/pclist")
    public R pcList(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.pcQueryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 修改手机用户审核状态 PC
     */
    @PostMapping("/pc/setstatus")
    public R resetStatus(@RequestBody UserEntity userEntity) {
        R rs=userService.resetStatus(userEntity);
        return rs;
    }

    /**
     * 删除手机用户账号 PC
     */
    @PostMapping("/pc/delphoneuser")
    public R delPhoneUser(@RequestBody Map<String, Object> params) {
        R rs=userService.delPhoneUser(params);
        return rs;
    }

    /**
     * 修改手机用户账号 PC
     */
    @PostMapping("/pc/resetphoneuser")
    public R resetPhoneUser(@RequestBody UserEntity userEntity) {
        R rs=userService.resetPhoneUser(userEntity);
        return rs;
    }

    /**
     * 添加手机用户账号 PC
     */
    @PostMapping("/pc/addphoneuser")
    public R addPhoneUser(@RequestBody UserEntity userEntity) {
        R rs=userService.addPhoneUser(userEntity);
        return rs;
    }

    /**
     * 添加农场管理员账号 PC
     */
    @PostMapping("/pc/addfarmuser")
    public R addFarmUser(@RequestBody Map<String, Object> params) {
        R rs=userService.addFarmUser(params);
        return rs;
    }

    /**
     * 修改农场管理员密码 PC
     */
    @PostMapping("/pc/resetfarmuser")
    public R resetFarmUser(@RequestBody UserEntity userEntity) {
        R rs=userService.resetFarmUser(userEntity);
        return rs;
    }

    /**
     * 删除农场管理员 PC
     */
    @PostMapping("/pc/delfarmuser")
    public R delFarmUser(@RequestBody Map<String, Object> params) {
        R rs=userService.delFarmUser(params);
        return rs;
    }


    /**
     * 删除手机管理员 PC
     */
    @PostMapping("/pc/del")
    public R del(@RequestBody Map<String, Object> params) {
        R rs=userService.del(params);
        return rs;
    }

    /**
     * 修改手机管理员密码 PC
     */
    @PostMapping("/pc/reset")
    public R reset(@RequestBody UserEntity userEntity) {
        R rs=userService.reset(userEntity);
        return rs;
    }

    /**
     * 添加手机管理员账号 PC
     */
    @PostMapping("/pc/add")
    public R add(@RequestBody UserEntity userEntity) {
        R rs=userService.add(userEntity);
        return rs;
    }

    /**
     * 手机管理员账号列表 PC
     */
    @GetMapping("/pc/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }

    // PC端登录
    @PostMapping("/pc/login")
    public R pcLogin(@RequestBody Map<String, Object> params){
        R rs=userService.pcLogin(params);
        return rs;
    }

    /**

     *查询在线人数

     */

    @RequestMapping("/pc/online")

    public Object online() {

        return "当前在线人数：" + MyHttpSessionListener.online + "人";

    }




    /**
     * 信息
     */
    @GetMapping("/info/{id}")
//    @PreAuthorize("hasAuthority('p1')")//拥有p1权限方可访问此url
    public R info(@PathVariable("id") Long id) {
        UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public R save(@Validated({AddGroupValid.class}) @RequestBody UserEntity user) {
        R saveRs = userService.register(user);
        return saveRs;
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody UserEntity user) {
        userService.updateById(user);
        return R.ok();
    }

    /**
     * 重置密码
     */
    @PutMapping("/resetPassword")
    public R resetPassword(@RequestBody Map<String, Object> params) {
        R rs=userService.resetPassword(params);
        return rs;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
